home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE14 / GETGLPOT.C < prev    next >
C/C++ Source or Header  |  1996-01-31  |  9KB  |  268 lines

  1.  
  2. #include <windows.h>  
  3. #include <math.h>
  4. #include "GetGlpOt.h" 
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName  = "MyApp";
  20. LPCTSTR lpszTitle    = "My Application"; 
  21.  
  22. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  23.  
  24. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  25.                       LPTSTR lpCmdLine, int nCmdShow)
  26. {
  27.    MSG      msg;
  28.    HWND     hWnd; 
  29.    WNDCLASS wc;
  30.  
  31.    // Register the main application window class.
  32.    //............................................
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    // Create the main application window.
  55.    //....................................
  56.    hWnd = CreateWindow( lpszAppName, 
  57.                         lpszTitle,    
  58.                         WS_OVERLAPPEDWINDOW, 
  59.                         CW_USEDEFAULT, 0, 
  60.                         CW_USEDEFAULT, 0,  
  61.                         NULL,              
  62.                         NULL,              
  63.                         hInstance,         
  64.                         NULL               
  65.                       );
  66.  
  67.    if ( !hWnd ) 
  68.       return( FALSE );
  69.  
  70.    ShowWindow( hWnd, nCmdShow ); 
  71.    UpdateWindow( hWnd );         
  72.  
  73.    while( GetMessage( &msg, NULL, 0, 0) )   
  74.    {
  75.       TranslateMessage( &msg ); 
  76.       DispatchMessage( &msg );  
  77.    }
  78.  
  79.    return( msg.wParam ); 
  80. }
  81.  
  82.  
  83. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  84. {
  85.    WNDCLASSEX wcex;
  86.  
  87.    wcex.style         = lpwc->style;
  88.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  89.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  90.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  91.    wcex.hInstance     = lpwc->hInstance;
  92.    wcex.hIcon         = lpwc->hIcon;
  93.    wcex.hCursor       = lpwc->hCursor;
  94.    wcex.hbrBackground = lpwc->hbrBackground;
  95.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  96.    wcex.lpszClassName = lpwc->lpszClassName;
  97.  
  98.    // Added elements for Windows 95.
  99.    //...............................
  100.    wcex.cbSize = sizeof(WNDCLASSEX);
  101.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  102.                             IMAGE_ICON, 16, 16,
  103.                             LR_DEFAULTCOLOR );
  104.             
  105.    return RegisterClassEx( &wcex );
  106. }
  107.  
  108.  
  109. // Convert degrees to radians.
  110. //............................
  111. #define RAD(x) ((x) * 3.1415927 / 180)
  112.  
  113. HBITMAP BitmapFromBits( void * lpBits, WORD width, WORD height )
  114. {
  115.     BITMAP bm;
  116.  
  117.     bm.bmType       = 0;
  118.     bm.bmWidth      = width;
  119.     bm.bmHeight     = height;
  120.     bm.bmWidthBytes = ((width + 31) >> 5) << 2;
  121.     bm.bmPlanes     = 1;
  122.     bm.bmBitsPixel  = 1;
  123.     bm.bmBits       = lpBits;
  124.  
  125.     return ( CreateBitmapIndirect( &bm ) );
  126. }
  127.  
  128. FIXED FixedFromDouble( double d )
  129. {
  130.     long l;
  131.  
  132.     l = (long) (d * 65536L);
  133.     return *(FIXED *)&l;
  134. }
  135.  
  136.  
  137. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  138. {
  139.    switch( uMsg )
  140.    {
  141.       case WM_COMMAND :
  142.               switch( LOWORD( wParam ) )
  143.               {
  144.                  case IDM_TEST :
  145.                         {
  146.                            HDC          hDC, hMemDC;
  147.                            HFONT        hFont, hOldFont;
  148.                            GLYPHMETRICS gm;
  149.                            MAT2         m2;
  150.                            DWORD        dwRet;
  151.                            LPBYTE       lpBuf;
  152.                            HBITMAP      hBitmap, hOldBmp;
  153.  
  154.                            // Create a Times New Roman font.
  155.                            //...............................
  156.                            hFont = CreateFont( 32, 0, 0, 0, FW_BOLD,
  157.                                                FALSE, FALSE, FALSE,
  158.                                                ANSI_CHARSET, 
  159.                                                OUT_DEFAULT_PRECIS,
  160.                                                CLIP_DEFAULT_PRECIS,
  161.                                                PROOF_QUALITY,
  162.                                                DEFAULT_PITCH | FF_DONTCARE,
  163.                                                "Times New Roman" );
  164.  
  165.  
  166.                            // Retrieve the device context,
  167.                            // select the font into it,
  168.                            // and create a compatible device context.
  169.                            //........................................
  170.                            hDC    = GetDC( hWnd );
  171.                            hMemDC = CreateCompatibleDC( hDC );
  172.                            hOldFont = SelectObject( hDC, hFont );
  173.                          
  174.                            // Set up the translation matrix to 
  175.                            // rotate the font 45 degrees.
  176.                            //................................. 
  177.                            m2.eM11 = FixedFromDouble(cos(RAD(45)));
  178.                            m2.eM12 = FixedFromDouble(sin(RAD(45)));
  179.                            m2.eM21 = FixedFromDouble(-sin(RAD(45)));
  180.                            m2.eM22 = FixedFromDouble(cos(RAD(45)));
  181.                            
  182.                            // Retrieve the size of the bitmap 
  183.                            // and allocate memory to hold it.
  184.                            //................................
  185.                            dwRet = GetGlyphOutline( hDC, 'A', GGO_BITMAP, 
  186.                                                     &gm, 0, NULL, &m2 );
  187.  
  188.                            lpBuf = GlobalAlloc( GPTR, dwRet );
  189.  
  190.                            // Retrieve the bitmap for the letter æAÆ.
  191.                            //........................................
  192.                            GetGlyphOutline( hDC, 'A', GGO_BITMAP, 
  193.                                             &gm, dwRet, lpBuf, &m2 );
  194.  
  195.                            // Create a HBITMAP and display it.
  196.                            //.................................
  197.                            hBitmap = BitmapFromBits( lpBuf, 
  198.                                                      (WORD)gm.gmBlackBoxX, 
  199.                                                      (WORD)gm.gmBlackBoxY );
  200.  
  201.                            hOldBmp = SelectObject( hMemDC, hBitmap );
  202.  
  203.                            BitBlt( hDC, 10, 
  204.                                         10, 
  205.                                         gm.gmBlackBoxX, 
  206.                                         gm.gmBlackBoxY, 
  207.                                         hMemDC, 0, 0, SRCCOPY);
  208.  
  209.                            // Output a normal æAÆ character.
  210.                            //...............................
  211.                            TextOut( hDC, 40, 10, "A", 1 );
  212.                                   
  213.                            // Clean up.
  214.                            //..........
  215.                            SelectObject( hMemDC, hOldBmp );
  216.                            DeleteObject( hBitmap );              
  217.                            GlobalFree( lpBuf );
  218.                            SelectObject( hDC, hOldFont );
  219.                            ReleaseDC( hWnd, hDC );
  220.                            DeleteObject( hFont );
  221.                         }
  222.                         break;
  223.  
  224.                  case IDM_ABOUT :
  225.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  226.                         break;
  227.  
  228.                  case IDM_EXIT :
  229.                         DestroyWindow( hWnd );
  230.                         break;
  231.               }
  232.               break;
  233.       
  234.       case WM_DESTROY :
  235.               PostQuitMessage(0);
  236.               break;
  237.  
  238.       default :
  239.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  240.    }
  241.  
  242.    return( 0L );
  243. }
  244.  
  245.  
  246. LRESULT CALLBACK About( HWND hDlg,           
  247.                         UINT message,        
  248.                         WPARAM wParam,       
  249.                         LPARAM lParam)
  250. {
  251.    switch (message) 
  252.    {
  253.        case WM_INITDIALOG: 
  254.                return (TRUE);
  255.  
  256.        case WM_COMMAND:                              
  257.                if (   LOWORD(wParam) == IDOK         
  258.                    || LOWORD(wParam) == IDCANCEL)    
  259.                {
  260.                        EndDialog(hDlg, TRUE);        
  261.                        return (TRUE);
  262.                }
  263.                break;
  264.    }
  265.  
  266.    return (FALSE); 
  267. }
  268.